package com.bipbip.repositorio;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bipbip.modelo.Producto;

@Repository(value = "productoDao")
@Transactional(readOnly = true)
public class JPAProductoDAO implements ProductoDAO {

	private EntityManager em = null;

	/*
	 * Sets the entity manager.
	 */
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	@Transactional(readOnly = true)
	public Producto getProductoBy(String attr, String value) {
		return (Producto) em.createQuery(
				"select p from Producto p where " + attr + "='" + value + "'")
				.getSingleResult();
	}

	@Transactional(readOnly = true)
	public Producto getProductoBy(String attr, Long value) {
		return (Producto) em.createQuery(
				"select p from Producto p where " + attr + "=" + value + "")
				.getSingleResult();
	}
}
